// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 1/30/2010 10:56:41 PM
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   DFMazeSolver.java


public class DFMazeSolver
    implements MazeSolver
{

    public DFMazeSolver()
    {
    }

    public MazeSolution solveMaze(Maze maze, MazeSolverListener mazesolverlistener)
    {
        MazeSolution mazesolution = MazeSolutionFactory.createMazeSolution(maze.getWidth(), maze.getHeight(), maze.getStartXPosition(), maze.getStartYPosition(), maze.getEndXPosition(), maze.getEndYPosition());
        mazesolverlistener.startingMazeSolution(mazesolution);
        boolean aflag[][] = new boolean[maze.getWidth()][maze.getHeight()];
        for(int i = 0; i < maze.getWidth(); i++)
        {
            for(int j = 0; j < maze.getHeight(); j++)
                aflag[i][j] = false;

        }

        aflag[maze.getStartXPosition()][maze.getStartYPosition()] = true;
        solve(mazesolution, aflag, maze, mazesolverlistener);
        return mazesolution;
    }

    private void solve(MazeSolution mazesolution, boolean aflag[][], Maze maze, MazeSolverListener mazesolverlistener)
    {
        int i = mazesolution.getLastXPosition();
        int j = mazesolution.getLastYPosition();
        if(!mazesolution.solutionComplete() && j > 0 && maze.canMoveNorthFrom(i, j) && !aflag[i][j - 1])
        {
            mazesolution.moveNorth();
            mazesolverlistener.solutionChanged(mazesolution);
            aflag[i][j - 1] = true;
            if(!mazesolution.solutionComplete())
                solve(mazesolution, aflag, maze, mazesolverlistener);
        }
        if(!mazesolution.solutionComplete() && j < maze.getHeight() - 1 && maze.canMoveSouthFrom(i, j) && !aflag[i][j + 1])
        {
            mazesolution.moveSouth();
            mazesolverlistener.solutionChanged(mazesolution);
            aflag[i][j + 1] = true;
            if(!mazesolution.solutionComplete())
                solve(mazesolution, aflag, maze, mazesolverlistener);
        }
        if(!mazesolution.solutionComplete() && i < maze.getWidth() - 1 && maze.canMoveEastFrom(i, j) && !aflag[i + 1][j])
        {
            mazesolution.moveEast();
            mazesolverlistener.solutionChanged(mazesolution);
            aflag[i + 1][j] = true;
            if(!mazesolution.solutionComplete())
                solve(mazesolution, aflag, maze, mazesolverlistener);
        }
        if(!mazesolution.solutionComplete() && i > 0 && maze.canMoveWestFrom(i, j) && !aflag[i - 1][j])
        {
            mazesolution.moveWest();
            mazesolverlistener.solutionChanged(mazesolution);
            aflag[i - 1][j] = true;
            if(!mazesolution.solutionComplete())
                solve(mazesolution, aflag, maze, mazesolverlistener);
        }
        if(!mazesolution.solutionComplete() && mazesolution.getLength() > 1)
        {
            mazesolution.backUp();
            mazesolverlistener.solutionChanged(mazesolution);
        }
    }
}
